<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/07/07 18:07:59 $)
// $RCSfile: staff_calendar.php,v $ : $Revision: 1.3 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_html.php");
require_once ("./includes/functions_customfields.php");
require_once ("./modules/teamwork/functions_teamwork.php");
require_once ("./modules/teamwork/functions_calendarrender.php");
require_once ("./modules/teamwork/functions_calendar.php");
$template->loadLanguageSection("staff_twcalendar");

if ($eventaction != "editevent")
{
	$interface->assignQuickLink($_SWIFT["language"]["day"], "index.php?_m=teamwork&_a=calendar&calendartype=day", "icon_caldayview.gif");
	$interface->assignQuickLink($_SWIFT["language"]["workweek"], "index.php?_m=teamwork&_a=calendar&calendartype=workweek", "icon_calworkweek.gif");
	$interface->assignQuickLink($_SWIFT["language"]["week"], "index.php?_m=teamwork&_a=calendar&calendartype=week", "icon_calweek.gif");
	$interface->assignQuickLink($_SWIFT["language"]["month"], "index.php?_m=teamwork&_a=calendar&calendartype=month", "icon_calmonth.gif");
}

/**
* ###############################################
* CALENDAR
* ###############################################
*/
if ($eventaction == "calendar")
{
	if (empty($_REQUEST["calendartype"]) || $_REQUEST["calendartype"] == "day")
	{
		$_calendartype = "day";
	} else if ($_REQUEST["calendartype"] == "week") {
		$_calendartype = "week";
	} else if ($_REQUEST["calendartype"] == "workweek") {
		$_calendartype = "workweek";
	} else if ($_REQUEST["calendartype"] == "month") {
		$_calendartype = "month";
	}

	$template->assign("calendar", true);
	$template->assign("isteamwork", true);

	if (!empty($_GET["customday"]))
	{
		$_customstore = explode("-", $_GET["customday"]);
		$template->assign("calfilterdate", $_customstore[2]."/".sprintf("%02d",$_customstore[1])."/".sprintf("%02d",$_customstore[0]));
	}

	if (!empty($_GET["insertevent"]))
	{
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["insertevent"], htmlspecialchars($_GET["insertevent"]));
	} else if (!empty($_GET["delevent"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["eventdelconfirm"], htmlspecialchars($_GET["delevent"]));
	}

	$interface->staffHeader($_SWIFT["language"]["teamwork"]." &gt; ".$_SWIFT["language"]["calendar"], 8);

	$interface->staffNavBar('<a href="index.php?_m=teamwork&_a=calendar" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=calendar" title="'.$_SWIFT["language"]["calendar"].'">'.$_SWIFT["language"]["calendar"].'</a>', "", 7);

	printInfoBox($_SWIFT["infomessage"]);
	printErrorBox($_SWIFT["errormessage"]);

	$loadingdata = '<table border="0" cellpadding="3" cellspacing="1" width="100%" class="tborder"><tr><td class="row1"><span class="smalltext"><img src="'. $_SWIFT["themepath"] .'loadingcircle.gif" border="0" align="absmiddle" />&nbsp;'. $_SWIFT["language"]["loading"] .'</span></td></tr></table><BR /> ';

	echo '<table cellspacing="0" cellpadding="0" border="0" width="100%" id="caltableopt"><tr style="height: 1em"><td align="left"><div id="calopt"><ul id="tab">
	
	<li><a'. iif($_calendartype=="day", ' class="currenttab"') .' href="#" onClick="this.blur(); switchGridTab(\'twday\', \'teamwork\');fetchData(\'twday\', \'0\');" id="twday" title="'. $_SWIFT["language"]["day"] .'">'. $_SWIFT["language"]["day"] .'</a></li>
	<li><a'. iif($_calendartype=="workweek", ' class="currenttab"') .' href="#" onClick="this.blur(); switchGridTab(\'twworkweek\', \'teamwork\');fetchData(\'twworkweek\', \'0\');" id="twworkweek" title="'. $_SWIFT["language"]["workweek"] .'">'. $_SWIFT["language"]["workweek"] .'</a></li>
	<li><a'. iif($_calendartype=="week", ' class="currenttab"') .' href="#" onClick="this.blur(); switchGridTab(\'twweek\', \'teamwork\');fetchData(\'twweek\', \'0\');" id="twweek" title="'. $_SWIFT["language"]["week"] .'">'. $_SWIFT["language"]["week"] .'</a></li>
	<li><a'. iif($_calendartype=="month", ' class="currenttab"') .' href="#" onClick="this.blur(); switchGridTab(\'twmonth\', \'teamwork\');fetchData(\'twmonth\', \'0\');" id="twmonth" title="'. $_SWIFT["language"]["month"] .'">'. $_SWIFT["language"]["month"] .'</a></li>
	</ul></div></td></tr>'.SWIFT_CRLF;

	echo '<tr style="height: 1em"><td align="left">'.SWIFT_CRLF;

	// ======= DAY DIV =======
	if ($_calendartype == "day")
	{
		if (!empty($_GET["customday"]))
		{
			$_customday = $_GET["customday"];
		} else {
			$_customday = false;
		}
		$dayresult = renderDayView($_customday);
	} else {
		$dayresult = $loadingdata;
	}
	echo '<div id="tab_twday" style="DISPLAY: '. iif($_calendartype=="day", "block", "none") .';" class="tabcontent">'. $dayresult .'</div>';

	// ======= WORK WEEK DIV =======
	if ($_calendartype == "workweek")
	{
		if (!empty($_GET["customweek"]))
		{
			$_customweek = $_GET["customweek"];
		} else {
			$_customweek = false;
		}
		$workweekresult = renderWorkWeekView($_customweek);
	} else {
		$workweekresult = $loadingdata;
	}
	echo '<div id="tab_twworkweek" style="DISPLAY: '. iif($_calendartype=="workweek", "block", "none") .';" class="tabcontent">'. $workweekresult .'</div>';

	// ======= WEEK DIV =======
	if ($_calendartype == "week")
	{
		if (!empty($_GET["customweek"]))
		{
			$_customweek = $_GET["customweek"];
		} else {
			$_customweek = false;
		}
		$weekresult = renderWorkWeekView($_customweek, true);
	} else {
		$weekresult = $loadingdata;
	}
	echo '<div id="tab_twweek" style="DISPLAY: '. iif($_calendartype=="week", "block", "none") .';" class="tabcontent">'. $weekresult .'</div>';


	// ======= MONTH DIV =======
	if ($_calendartype == "month")
	{
		if (!empty($_GET["custommonth"]))
		{
			$_custommonth = $_GET["custommonth"];
		} else {
			$_custommonth = false;
		}
		$monthresult = renderMonthView($_custommonth);
	} else {
		$monthresult = $loadingdata;
	}
	echo '<div id="tab_twmonth" style="DISPLAY: '. iif($_calendartype=="month", "block", "none") .';" class="tabcontent">'. $monthresult .'</div>';

	echo '</td></tr></table>';

	$interface->staffFooter();







/**
* ###############################################
* INSERT EVENT
* ###############################################
*/
} else if ($eventaction == "insertevent") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["subject"]) == "")
		{
			$errormessage = $_SWIFT["language"]["subjectempty"];
		} else if (empty($_POST["startdateline"]) || !checkCustomFieldContent(CUSTOMFIELD_EVENT)) {
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			$_startdateline = calenderDateTimeToDateline($_POST["startdateline"], $_POST["startdatelinetime"]);
			if (empty($_POST["enddateline"]))
			{
				$_enddateline = $_startdateline+(30*60);
			} else {
				$_enddateline = calenderDateTimeToDateline($_POST["enddateline"], $_POST["enddatelinetime"]);
			}
			if ($_enddateline < $_startdateline)
			{
				$_enddateline = $_startdateline+(30*60);
			}

			if ($_POST["eventtype"] == SWIFTPRIVATE)
			{
				$_POST["ownerstaffid"] = $_SWIFT["staff"]["staffid"];
			}

			if ($_POST["isalldayevent"] == "1")
			{
				$_startdateline = mktime(12,0,0,date("m", $_startdateline), date("d", $_startdateline), date("Y", $_startdateline));
				$_enddateline = $_startdateline;
			}

			$calendareventid = insertEvent($_POST["subject"], $_POST["eventtype"], $_POST["ownerstaffid"], $_POST["isalldayevent"], $_startdateline, $_enddateline, $_POST["activitytype"], $_POST["location"], $_POST["body"], $_POST["calendarstatusid"], $_POST["calendarlabelid"], $_POST["calendarcategoryid"]);

			if ($calendareventid)
			{
				updateCustomFields(CUSTOMFIELD_EVENT, $calendareventid);
			}

			printRedirect(sprintf($_SWIFT["language"]["eventinsertconfirm"], htmlspecialchars($_POST["subject"])), "index.php?_m=teamwork&_a=calendar&insertevent=".urlencode($_POST["subject"])."&customday=".date("d-m-Y", $_startdateline));
			exit;
		}
	}

	$template->assign("calendar", true);

	$interface->staffHeader($_SWIFT["language"]["teamwork"]." &gt; ".$_SWIFT["language"]["insertevent"], 8);

	$interface->staffNavBar('<a href="index.php?_m=teamwork&_a=calendar" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=calendar" title="'.$_SWIFT["language"]["calendar"].'">'.$_SWIFT["language"]["calendar"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=insertevent" title="'.$_SWIFT["language"]["insertevent"].'">'.$_SWIFT["language"]["insertevent"].'</a>', "", 7);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderEventForm(INSERT);

	$template->assign("backurl", "index.php?_m=teamwork&_a=calendar");

	$interface->staffFooter();








/**
* ###############################################
* EDIT EVENT
* ###############################################
*/
} else if ($eventaction == "editevent") {
	$_event = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."calendarevents` AS calendarevents LEFT JOIN `". TABLE_PREFIX ."calendardata` AS calendardata ON (calendardata.datatype = '". TEAMWORK_EVENT ."' AND calendardata.typeid = calendarevents.calendareventid) WHERE calendarevents.calendareventid = '". intval($_REQUEST["calendareventid"]) ."';");
	if (empty($_event["calendareventid"]) || ($_event["eventtype"] == SWIFTPRIVATE && $_event["staffid"] != $_SWIFT["staff"]["staffid"]))
	{
		trigger_error($_SWIFT["language"]["invalidevent"], E_USER_ERROR);
	}

	if ($_GET["mode"] == "delete")
	{
		deleteCalendarEvents(array($_event["calendareventid"]));

		printRedirect(sprintf($_SWIFT["language"]["eventdelconfirm"], htmlspecialchars($_event["subject"])), "index.php?_m=teamwork&_a=calendar&delevent=".urlencode($_event["subject"])."&customday=".date("d-m-Y", $_event["startdateline"]));
		exit;
	}

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["subject"]) == "")
		{
			$errormessage = $_SWIFT["language"]["subjectempty"];
		} else if (empty($_POST["startdateline"]) || !checkCustomFieldContent(CUSTOMFIELD_EVENT)) {
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			$_startdateline = calenderDateTimeToDateline($_POST["startdateline"], $_POST["startdatelinetime"]);
			if (empty($_POST["enddateline"]))
			{
				$_enddateline = $_startdateline+(30*60);
			} else {
				$_enddateline = calenderDateTimeToDateline($_POST["enddateline"], $_POST["enddatelinetime"]);
			}
			if ($_enddateline < $_startdateline)
			{
				$_enddateline = $_startdateline+(30*60);
			}

			if ($_POST["eventtype"] == SWIFTPRIVATE)
			{
				$_POST["ownerstaffid"] = $_SWIFT["staff"]["staffid"];
			}

			if ($_POST["isalldayevent"] == "1")
			{
				$_startdateline = mktime(12,0,0,date("m", $_startdateline), date("d", $_startdateline), date("Y", $_startdateline));
				$_enddateline = $_startdateline;
			}

			updateEvent($_event["calendareventid"], $_POST["subject"], $_POST["eventtype"], $_POST["ownerstaffid"], $_POST["isalldayevent"], $_startdateline, $_enddateline, $_POST["activitytype"], $_POST["location"], $_POST["body"], $_POST["calendarstatusid"], $_POST["calendarlabelid"], $_POST["calendarcategoryid"]);
			updateCustomFields(CUSTOMFIELD_EVENT, $_event["calendareventid"]);

			printRedirect(sprintf($_SWIFT["language"]["eventupdateconfirm"], htmlspecialchars($_POST["subject"])), "index.php?_m=teamwork&_a=calendar&updateevent=".urlencode($_POST["subject"])."&customday=".date("d-m-Y", $_startdateline));
			exit;
		}
	}

	$_POST = array_merge($_POST, $_event);

	$template->assign("calendar", true);

	if ($_REQUEST["mode"] == "edit")
	{
		$modetext = $_SWIFT["language"]["editevent"];
	} else {
		$modetext = $_SWIFT["language"]["viewevent"];
	}

	$template->assign("itemoptiontitle", $_SWIFT["language"]["eventoptions"]);
	if ($_REQUEST["mode"] == "edit")
	{
		$interface->assignItemOption($_SWIFT["language"]["viewevent"], "index.php?_m=teamwork&_a=editevent&calendareventid=".intval($_event["calendareventid"])."&mode=view", "calendar.gif");
	} else {
		$interface->assignItemOption($_SWIFT["language"]["editevent"], "index.php?_m=teamwork&_a=editevent&calendareventid=".intval($_event["calendareventid"])."&mode=edit", "calendar.gif");
	}
	$interface->assignItemOption($_SWIFT["language"]["deleteevent"], "", "icon_delete.gif", "", 'doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=teamwork&_a=editevent&calendareventid='.intval($_event["calendareventid"]).'&mode=delete\');');

	$interface->staffHeader($_SWIFT["language"]["teamwork"]." &gt; ".$modetext, 8);

	$interface->staffNavBar('<a href="index.php?_m=teamwork&_a=calendar" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=calendar&customday='.date("d-m-Y", $_event["startdateline"]) .'" title="'.$_SWIFT["language"]["calendar"].'">'.$_SWIFT["language"]["calendar"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=editevent&calendareventid='. intval($_event["calendareventid"]) .'" title="'.$modetext.'">'.$modetext.' ('. htmlspecialchars($_event["subject"]) .')</a>', "", 7);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderEventForm(iif($_REQUEST["mode"]=="edit", EDIT, DISPLAY));

	$template->assign("backurl", "index.php?_m=teamwork&_a=calendar&customday=".date("d-m-Y", $_event["startdateline"]));

	$interface->staffFooter();
}


?>